<?php

namespace Admin\Model\DBO;

use Zend\Db\TableGateway\AbstractTableGateway;
use Admin\Entity\Reader;

class ReaderDBO extends AbstractTableGateway {

    private $sm;
    private $readerTable;

    public function __construct($serviceManager) {
        $this->sm = $serviceManager;
    }

    public function getReaderTable() {
        if (!$this->readerTable) {
            $this->readerTable = $this->sm->get('Admin\Model\ReaderTable');
        }
        return $this->readerTable;
    }

    public function getInfoReaderFromUserID($userID) {
        return $this->getReaderTable()->getInfoReaderFromUserID($userID);
    }

    public function getInfoReaderFromBarcode($cardBarcode) {
        return $this->getReaderTable()->getInfoReader($cardBarcode);
    }

    public function fetchAllReader($paginated = false, $value = 4) {
        $paginator = $this->getReaderTable()->fetchAllReader($paginated, $value);
        return $paginator;
    }

    public function getAllNewReader() {
        $iteratorAdapter = new \Zend\Paginator\Adapter\Iterator($this->getReaderTable()->getAllNewReader());
        $paginator = new \Zend\Paginator\Paginator($iteratorAdapter);
        return $paginator;
    }

    public function saveReader(Reader $reader) {
        // lay ngay gio cua he thong
        $date = date('Y-m-d');
        $reader->dateCreated = $date;
        $ciculationPolicy = $this->getReaderTable()->getCirculationPolicy('TGLT');
        $dateExpiry = new \DateTime($date);
        date_add($dateExpiry, date_interval_create_from_date_string($ciculationPolicy['amount'] . 'days'));
        $dateExpiry = $dateExpiry->format('Y-m-d');
        $reader->dateExpiry = $dateExpiry;
        return $this->getReaderTable()->saveReader($reader);
    }

    public function updateReaderInfo(Reader $reader, $userID) {
        $this->getReaderTable()->updateReaderInfo($reader, $userID);
    }

    public function changeIsActiveStatus($isActive, $userID) {
        $this->getReaderTable()->changeIsActiveStatus($isActive, $userID);
    }

}

?>